package daniel.taotao.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import daniel.taotao.common.utils.JsonUtils;
import daniel.taotao.utils.FastDFSClient;

@Controller
public class PictureController {
	@Value("${IMAGE_SERVER_URL}")
	private String IMAGE_SERVER_URL;
	
	@RequestMapping("/pic/upload")
	@ResponseBody//浏览器不兼容 用jaonutils转为json
	public String uploadFile(MultipartFile uploadFile){
		try{
		String originalFilename = uploadFile.getOriginalFilename();
		System.out.println(uploadFile.getOriginalFilename()+"-----------");
		String name=originalFilename.substring(originalFilename.lastIndexOf(".")+1);
		FastDFSClient fastDFSClient = new FastDFSClient("classpath:tracker/client.conf");
		String url = fastDFSClient.uploadFile(uploadFile.getBytes(), name);
		System.out.println(url);
		url=IMAGE_SERVER_URL+url;
		System.out.println(url);
		Map map=new HashMap<>();
		map.put("error", 0);
		map.put("url", url);
		return JsonUtils.objectToJson(map);
		}catch (Exception e){
			e.printStackTrace();
			Map map=new HashMap<>();
			map.put("error", 1);
			map.put("message", "上传出错");
			return JsonUtils.objectToJson(map);
		}
		
	}
}
